package com.pearlsoft.webapp.rms.vappmgr;

import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import com.pearlsoft.rms.VcloudManager;
import com.pearlsoft.webapp.core.annotation.DataSetHandlerComponent;
import com.pearlsoft.webapp.core.dataset.DataSet;
import com.pearlsoft.webapp.core.dataset.Result;
import com.pearlsoft.webapp.core.web.basehandler.AbsDataSetHandler;
import com.vmware.vcloud.sdk.Organization;
import com.vmware.vcloud.sdk.VM;
import com.vmware.vcloud.sdk.Vapp;
import com.vmware.vcloud.sdk.Vdc;

@DataSetHandlerComponent(name = "VappMgrDatasetHandler")
public class VappMgrDatasetHandler extends AbsDataSetHandler {
	


	@Override
	public void delete(DataSet ds, Result result) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void deleteAll(DataSet ds, Result result) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void save(DataSet ds, Result result) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void handler(DataSet ds, Result result) throws Exception {
		// TODO Auto-generated method stub
		
	}
	
	public void powerOnVapp(final DataSet ds, Result result) throws Exception {
	

		String vApp_Name = (String)ds.getConditions().get("VAPP_NAME");
		String vApp_Vdc = ds.getConditions().get("VAPP_VDC").toString();
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vApp_Vdc);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);

		if(!vapp.equals(null)&& !vapp.getVappStatus().toString().equals("POWERED_ON"))
		{
			vapp.powerOn();
		}
				
	}
	
	public void shutdownVapp(final DataSet ds, Result result) throws Exception {
		

		String vApp_Name = (String)ds.getConditions().get("VAPP_NAME");
		String vApp_Vdc = ds.getConditions().get("VAPP_VDC").toString();
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vApp_Vdc);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		
	
		if(!vapp.equals(null))
		{
		
			if(vapp.getVappStatus().toString().equals("POWERED_ON"))
			{
		    	vapp.shutdown();
			}
		}
				
		
	}
	
	public void suspendVapp(final DataSet ds, Result result) throws Exception {
		

		String vApp_Name = (String)ds.getConditions().get("VAPP_NAME");
		String vApp_Vdc = ds.getConditions().get("VAPP_VDC").toString();
		VcloudManager vcm = this.getBean("vcloudManager", VcloudManager.class);
		vcm.setLogLevel(Level.OFF);
		//vcm.login("https://192.168.0.7", "administrator@test", "abc123..");
		Map<String, Organization> orgs = vcm.getOrgMap();
		Organization org = orgs.get(System.getProperty("ORG_NAME"));
		Map<String, Vdc> vdcs = vcm.getVdcMap(org);
		Vdc vdc = vdcs.get(vApp_Vdc);
		Map<String, Vapp> vapps = vcm.getVappMap(vdc);
		Vapp vapp = vapps.get(vApp_Name);
		
		if(!vapp.equals(null))
		{
			if(vapp.getVappStatus().toString().equals("POWERED_ON"))
			{
				vapp.suspend();
			}
			
		}
		
	}

}
